#include "tunnel_func.h"
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdio.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <signal.h>
#include <string.h>

using namespace std;

#define PORT 8080
#define ADDRESS "127.0.0.1"

// SOCKS Messages Structures
struct handshakeMessage{
	uint8_t version; // SOCKS5 - 0x05
	uint8_t nmethods; // v tomto pripade dame iba jednu myslim
	uint8_t methods; // cislo 0
};

#pragma pack(1)
struct requestIPv4Message{
	uint8_t version;
	uint8_t command;
	uint8_t rsv;
	uint8_t atype;
	uint32_t addr;
	uint16_t port;
};
#pragma pack() // set to default

int main(){

	// ZISTENIE ADRESY GOOGLE A VYPISANIE
	struct addrinfo *adresa;
	char hostname[] = "www.l.google.com";
	if(getaddrinfo(hostname, NULL, NULL, &adresa) != 0){
		perror("getaddrinfo");
	}
	printf("adresa: %s\n", inet_ntoa((*(struct sockaddr_in *)(adresa->ai_addr)).sin_addr));

	// ukoncenie prace s addrinfo
	freeaddrinfo(adresa);
	
	int sock = socket(AF_INET, SOCK_STREAM, 0);

	string addr(ADDRESS);
	sockaddr_in address;

  printf("proxy na: %s:%d\n", addr.c_str(), PORT);

	// TARGET PROPERTIES
	address.sin_family = AF_INET;
	address.sin_addr.s_addr = inet_addr(addr.c_str());
	address.sin_port = htons(PORT);	

	// HANDSHAKE

	handshakeMessage handshake;
	handshake.version = 0x05;
	handshake.nmethods = 1;
	handshake.methods = 0;
	printf("%lu \n", sizeof(handshake));

	if(connect(sock, (struct sockaddr *) &address, (socklen_t)sizeof(sockaddr)) == -1){
		perror("connect");
    exit(1);
  }

	if(send(sock, &handshake, sizeof(handshake), 0) == -1){
		perror("send");
	}

	char  * response = new char[2];
	response = (char *)safeRead(sock, 2);
	printf("response version: %d, chosen method: %d\n", response[0], response[1]);
	delete [] response;
	/*
	if(read(sock, &response, 2) != -1){
		printf("response version: %d, chosen method: %d\n", response[0], response[1]);
	}
	else
		perror("read");
		*/
	
	// REQUEST

	//printf("req size: %lu\n", sizeof(struct requestIPv4Message));

	requestIPv4Message request;
	request.version = 0x05;
	request.command = 0x03;
	request.rsv = 0;
	request.atype = 0x01;
	inet_aton("127.0.0.1",(in_addr*)&request.addr);
	request.port = htons(8088);

	if(send(sock, &request, sizeof(struct requestIPv4Message), 0) == -1)
		perror("send");

	// REQ RESPONSE
	int size = 0;
	if(read(sock, &request, 4) == -1)
	{
		perror("read request response");
		exit(1);
	}

	switch(request.atype)
	{
		case 1: size = 4; break;
		case 4: size = 8;
		case 3: read(sock, &size, 1); 
		default: 
			if(size == -1)
				perror("read size");
	}
	
	char tmpadresa[size + 1];
	read(sock, &tmpadresa, size);
	tmpadresa[size] = '\0';
	
	read(sock, &request.port, 2);


	// debugvypisy
	printf("prot: %d, repcode: %d, atype: %d\n", request.version, request.command, request.atype);

	if(request.command == 0)
	{

		// MESSAGE and response
		char sprava[] = "Cvokado si posielam";

		if(send(sock, sprava, sizeof(sprava), 0) == -1)
			perror("send message");

		// DALSIA sprava pre poriadok
		char sprava2[] = "Drakoooo";

		if(send(sock, sprava2, sizeof(sprava2), 0) == -1)
			perror("send message");

		shutdown(sock, SHUT_WR);
		
		puts("am here");
		char buf[1024];
		int bytes;
		puts("vratilo sa");
		while((bytes = read(sock, &buf, 1023)) > 0){
			buf[bytes] = '\0';
			printf("prijatych (%d): ", bytes);
			write(1, buf, bytes);

		}
		if(bytes == -1)
			perror("read response");

		puts("");
	}
	else
	{
		puts("repcode non-zero");
	}

	close(sock);

	return 0;
}
